package com.pronosticnews.client.admin.view.editStade;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.pronosticnews.client.admin.resources.PronosticnewsAdminMessages;
import com.pronosticnews.client.admin.resources.PronosticnewsAdminResources;
import com.pronosticnews.shared.model.StadeDto;

public class EditStadeViewImpl extends Composite implements EditStadeView {

	interface EditStadeUiBinder extends UiBinder<Widget, EditStadeViewImpl> {
	}

	private Presenter presenter;
	
	@UiField
	TextBox txtImage;
	
	@UiField
	Image imgLogo;

	@UiField
	FormPanel form;

	@UiField
	FileUpload uplImage;

	@UiField
	TextBox txtNom;

	@UiField
	Label lblTitle;
	
	@UiField
	HorizontalPanel panNouveau;
	
	@UiField
	HorizontalPanel panModifier;
	
	@UiField
	VerticalPanel containerForm;
	
	@UiField(provided=true) 
	PronosticnewsAdminResources res;
 	
	PronosticnewsAdminMessages messages;

	private StadeDto editStade;

	private final Integer logoSize = 100;

	public EditStadeViewImpl() {
		messages = GWT.create(PronosticnewsAdminMessages.class);
		res = GWT.create(PronosticnewsAdminResources.class);
		res.cssFormEdition().ensureInjected();
		initWidget(GWT.<EditStadeUiBinder> create(EditStadeUiBinder.class)
				.createAndBindUi(this));
		lblTitle.addStyleName(res.cssFormEdition().title_creation());
		initForm();
		}

		private void initForm() {
			form.setAction(com.google.gwt.core.client.GWT.getHostPageBaseURL()+"image/");
			form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
				@Override
				public void onSubmitComplete(SubmitCompleteEvent event) {
					try {					
						Integer idImage = Integer.parseInt(event.getResults().replace("<pre>", "").replace("</pre>", ""));
						if (idImage != null) {
							editStade.setPhoto(idImage);
						}
						presenter.submit(editStade);
					} catch (Exception e) {
					}
				}
			});
		}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void submitComplete() {
		clearFormulaire();
	}

	private void clearFormulaire() {
		imgLogo.setVisible(false);
		txtImage.setText("");
		txtImage.setValue("");
		txtNom.setValue("");
	}

	@Override
	public void setStade(StadeDto existingStade) {
		if (existingStade.getPhoto() != null) {
			txtImage.setText(existingStade.getPhoto().toString());
			imgLogo.setUrl(com.google.gwt.core.client.GWT.getHostPageBaseURL()+"image/?id=" + existingStade.getPhoto()+"&height="+logoSize);
			imgLogo.setVisible(true);
		}
		txtNom.setText(existingStade.getNom());
	}

	@Override
	public void setModeModification(boolean mode) {
		panNouveau.setVisible(!mode);
		panModifier.setVisible(mode);
		clearFormulaire();
		if (mode) {
			lblTitle.setText(messages.modifier_stade());
			lblTitle.addStyleName(res.cssFormEdition().title_modif());
			lblTitle.removeStyleName(res.cssFormEdition().title_creation());
		}
		else {
			lblTitle.setText(messages.enregistrer_stade());
			lblTitle.addStyleName(res.cssFormEdition().title_creation());
			lblTitle.removeStyleName(res.cssFormEdition().title_modif());
		}
	}

	@UiHandler({"btEnregistrer", "btModifier"})
	void onClick(ClickEvent event) {
		editStade = new StadeDto();
		editStade.setNom(txtNom.getValue());
		if (!"".equals(uplImage.getFilename())) {
			form.submit();
		}
		else {
			presenter.submit(editStade);
		}		
		setModeModification(false);
	}

	@UiHandler("btAnnuler") 
	void doClickAnnuler(ClickEvent event) {
		setModeModification(false);
		presenter.annuleModification();
	}
	
}
